﻿using Common.Utils;
using Common.Utils.Helper;
using Game.Kernel;
using Game.Logic.Data;
using Game.Logic.Events;
using Game.Logic.Manager;
using UIFramework;
using UnityEngine.UI;

namespace Game.Logic.Login
{
    public class GameLoadingView : GameUIView
    {
        private Scrollbar m_loadingBar;
        private Text m_title;
        private Text m_tip1;
        private Text m_tip2;

        void Awake()
        {
            RegisterGameUIEvent();
            m_loadingBar = UnityHelper.GetTheChildComponent<Scrollbar>(gameObject, "LoadingBar");
            m_title = UnityHelper.GetTheChildComponent<Text>(gameObject, "Title");
            m_tip1 = UnityHelper.GetTheChildComponent<Text>(gameObject, "Tip_1");
            m_tip2 = UnityHelper.GetTheChildComponent<Text>(gameObject, "Tip_2");
            Reset();
        }

        protected override void RegisterGameUIEvent()
        {
            GameUIEnterEvent = GameUIEvent.Create(GameEventType.GameUIEnter, ENUM_UITYPE.GAMELOADING);
        }

        private void Reset()
        {
            m_tip1.gameObject.SetActive(false);
            m_tip2.gameObject.SetActive(false);
        }

        public override void OnEnter(params object[] _params)
        {
            GameEventManager.Instance.DispatchEvent(GameUIEnterEvent);
        }

        public override void OnExit()
        {

        }

        public override void OnPause()
        {

        }

        public override void OnResume()
        {

        }

        private void DisplayVersionInfo()
        {
            if (null != GameVersionManager.Instance.ClientVersion)
            {
                m_tip1.gameObject.SetActive(true);
                m_tip1.text = string.Concat("客户端版本：", GameVersionManager.Instance.ClientVersion.GetVersionStr());
            }

            if (null != GameVersionManager.Instance.ServerVersion)
            {
                m_tip2.gameObject.SetActive(true);
                m_tip2.text = string.Concat("服务器版本：", GameVersionManager.Instance.ServerVersion.GetVersionStr());
            }
        }

        private void OnRecvVersionData(GameBaseModelEvent _gme)
        {
            GameLoadingModelEvent glme = _gme.Cast<GameLoadingModelEvent>();
            switch(glme.Status)
            {
                case GameStatus.Init:
                    Reset();
                    DisplayVersionInfo();
                    m_title.text = "正在初始化游戏...";
                    break;
                case GameStatus.ConnectServer:
                    Reset();
                    DisplayVersionInfo();
                    m_title.text = "正在连接服务器...";
                    break;
                case GameStatus.GetVersion:
                    Reset();
                    DisplayVersionInfo();
                    m_title.text = "正在获取游戏版本信息...";
                    break;
                case GameStatus.CheckVersion:
                    Reset();
                    DisplayVersionInfo();
                    m_title.text = "正在验证游戏版本信息...";
                    break;
                case GameStatus.Downloading:
                    Reset();
                    DisplayVersionInfo();
                    if (glme.Progress > 0.0f)
                        m_title.text = string.Format(GameLangueManager.Instance.LoadText("TEXT_GAME_UPDATE_RES"), Convert.ToPercent(glme.Progress));
                    m_loadingBar.size = glme.Progress;
                    break;
                default:
                    break;
            }

        }

        public override void OnModelEventHandler(GameBaseModelEvent _event)
        {
            GameModelEvent gme = _event.Cast<GameModelEvent>();
            switch (gme.DataType)
            {
                case GameDataType.GameVersion:
                    OnRecvVersionData(gme);
                    break;
                default:
                    break;
            }
        }

    }
}

